package com.kqb.controller;


import cn.hutool.core.bean.BeanUtil;
import com.kqb.common.domain.AjaxResult;
import com.kqb.common.domain.ErrorResult;
import com.kqb.common.domain.page.PageResult;
import com.kqb.common.group.ApiGroups;
import com.kqb.pojo.dto.ForwarderDto;
import com.kqb.query.ForwarderQuery;
import com.kqb.service.ForwarderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/api")
@Api(tags = "货代管理")

public class ForwarderController {

    @Autowired
    ForwarderService forwarderService;

    @ApiOperation("保存货代")
    @PostMapping("/forwarder")
    public AjaxResult save(@Validated(ApiGroups.Save.class) @RequestBody ForwarderDto forwarder, BindingResult result) {
        ErrorResult.validate(result);
        int save = forwarderService.save(forwarder);
        return AjaxResult.success("保存成功", save);
    }

    
    @ApiOperation("查询货代")
    @GetMapping("/forwarder/{id}")
    public AjaxResult get( @PathVariable(required = true) int id) {
        ForwarderDto select = forwarderService.select(id);
        return AjaxResult.success("货代信息", select);
    }

    @ApiOperation(value = "查询货代列表")
    @PostMapping("/forwarder/list")
    public AjaxResult query(@RequestBody ForwarderQuery query) {

        System.out.println(query);
        PageResult result = forwarderService.queryByMap(BeanUtil.beanToMap(query));
        return AjaxResult.success("货代列表", result);
    }

    @ApiOperation(value = "修改货代")
    @PutMapping("/forwarder")
    public AjaxResult update(@RequestBody ForwarderDto forwarderDTO) {

        System.out.println("forwarderDTO:"+forwarderDTO);
        int update = forwarderService.update(forwarderDTO);
        return AjaxResult.success("修改成功",update);
    }

    @ApiImplicitParam(name="id",value = "货代id",dataTypeClass = Integer.class,paramType = "query",required = true )
    @ApiOperation("删除货代")
    @DeleteMapping("/forwarder")
    public AjaxResult delete(@RequestParam(required = true) int id) {
        int rs = forwarderService.delete(id);
        return AjaxResult.success("删除成功。",rs);
    }
}
